{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 列表转为ndarray",
   "id": "51fc91488b24c78f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T15:13:47.470284Z",
     "start_time": "2025-01-06T15:13:47.465472Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "list_one = [1, 2, 3, 4, 5]\n",
    "print(list_one)  # [1, 2, 3, 4, 5]\n",
    "print(type(list_one))  # <class 'list'>\n",
    "\n",
    "oneArray = np.array(list_one)  # 列表转为ndarray\n",
    "print(oneArray)  # [1 2 3 4 5]  注意：没有逗号\n",
    "print(type(oneArray))  # <class 'numpy.ndarray'>"
   ],
   "id": "7f0ccddaae337eb3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "<class 'list'>\n",
      "[1 2 3 4 5]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray与Python列表运算效率对比",
   "id": "f99a38f0c5e6aefe"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T15:14:03.647611Z",
     "start_time": "2025-01-06T15:13:47.487294Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import time\n",
    "import random\n",
    "import numpy as np\n",
    "\n",
    "a = []\n",
    "for i in range(10 ** 8):\n",
    "    a.append(random.random())  # 随机生成10^8个元素的列表\n",
    "b = np.array(a)  # 列表转为ndarray\n",
    "\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)  # Python列表求和\n",
    "t2 = time.time()\n",
    "\n",
    "t3 = time.time()\n",
    "sum2 = np.sum(b)  # ndarray求和\n",
    "t4 = time.time()\n",
    "\n",
    "print(\"Python列表求和耗时：\", t2 - t1)\n",
    "# Python列表求和耗时： 0.5247118473052979\n",
    "print(\"ndarray求和耗时：\", t4 - t3)\n",
    "# ndarray求和耗时： 0.5247118473052979"
   ],
   "id": "aaa3750331c7e741",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python列表求和耗时： 0.5616576671600342\n",
      "ndarray求和耗时： 0.11401939392089844\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T15:14:40.558057Z",
     "start_time": "2025-01-06T15:14:40.554281Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(type(range(10)))  # <class 'range'>\n",
    "# range返回的是一个range对象，不能直接转换为ndarray\n",
    "oneArray = np.array(range(10))\n",
    "print(oneArray)  # [0 1 2 3 4 5 6 7 8 9]\n",
    "print(type(oneArray))  # <class 'numpy.ndarray'>"
   ],
   "id": "d78478ff7095289b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T15:20:44.785756Z",
     "start_time": "2025-01-06T15:20:44.780549Z"
    }
   },
   "cell_type": "code",
   "source": [
    "twoArray = np.arange(0, 10, 2)  # 等价于range(0,10,2)，返回ndarray\n",
    "print(twoArray)  # [0 2 4 6 8]\n",
    "print(type(twoArray))  # <class 'numpy.ndarray'>"
   ],
   "id": "a52d5d65406bad77",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T04:18:59.529295Z",
     "start_time": "2025-01-07T04:18:59.523491Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二维列表转ndarray\n",
    "list_two = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "print(list_two)  # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "print(type(list_two))  # <class 'list'>\n",
    "\n",
    "twoArray = np.array(list_two)\n",
    "print(twoArray)  # [[1 2 3]\n",
    "#  [4 5 6]\n",
    "#  [7 8 9]]\n",
    "print(type(twoArray))  # <class 'numpy.ndarray'>\n",
    "\n",
    "list_three = twoArray.tolist()  # ndarray转为列表\n",
    "print(list_three)  # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "print(type(list_three))  # <class 'list'>"
   ],
   "id": "77c6ff32d930d047",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
      "<class 'list'>\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "<class 'numpy.ndarray'>\n",
      "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.3 常用属性",
   "id": "b358ebaa6307743e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T15:30:51.139478Z",
     "start_time": "2025-01-06T15:30:51.134997Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#ndarray的各种属性\n",
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "\n",
    "threeDArray = np.array(list2)\n",
    "\n",
    "# 获取数组的维度( 注意： 与函数的参数很像) \n",
    "print(threeDArray.ndim)  # 2  维度\n",
    "# 形状（行，列） (0 axis,1 axis)\n",
    "print(threeDArray.shape)  # (3, 2)  维度\n",
    "# 有多少个元素\n",
    "print(threeDArray.size)  # 6  元素个数\n",
    "# 数据类型\n",
    "print(threeDArray.dtype)  # int64  数据类型\n",
    "print(threeDArray.itemsize)  # 8  每个元素的字节数"
   ],
   "id": "30805a4a6173ced0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n",
      "int64\n",
      "8\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.4调整数组的形状",
   "id": "313b41ffa22a8671"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T15:40:38.057595Z",
     "start_time": "2025-01-06T15:40:38.052603Z"
    }
   },
   "cell_type": "code",
   "source": [
    "fourDArray = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "print(fourDArray)\n",
    "# [[1 2 3]\n",
    "#  [4 5 6]]\n",
    "fourDArray_copy = fourDArray\n",
    "print(fourDArray_copy)\n",
    "# [[1 2 3]\n",
    "#  [4 5 6]]\n",
    "# 地址相同，说明指向同一块内存空间\n",
    "print(id(fourDArray))  #2465659748976\n",
    "print(id(fourDArray_copy))  #2465659748976\n",
    "\n",
    "fourDArray.shape = (3, 2)  # 调整形状\n",
    "print(fourDArray)\n",
    "# [[1 2]\n",
    "#  [3 4]\n",
    "#  [5 6]]\n",
    "# 地址相同，说明指向同一块内存空间，即简单copy和调整形状操作都是在相同内存上修改\n",
    "print(id(fourDArray))  #2465659748976\n",
    "\n",
    "fourDArray_test = fourDArray.reshape(3, 2)  # 调整形状\n",
    "print(fourDArray_test)\n",
    "# [[1 2]\n",
    "#  [3 4]\n",
    "#  [5 6]]\n",
    "# 返回一个新的数组，reshape后id和之前的不一样\n",
    "print(id(fourDArray_test))  #2465659749264"
   ],
   "id": "e8f718d998fe35fc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "2465659748976\n",
      "2465659748976\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "2465659748976\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "2465659749264\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T15:53:31.138530Z",
     "start_time": "2025-01-06T15:53:31.133418Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将多维变成一维数组\n",
    "# 默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "fiveDArray = fourDArray.reshape((fourDArray.size,), order='C')\n",
    "print(fiveDArray)  # [1 2 3 4 5 6]\n",
    "fiveDArray = fourDArray.reshape((fourDArray.size,), order='F')\n",
    "print(fiveDArray)  # [1 4 2 5 3 6]\n",
    "\n",
    "sixDArray = fourDArray.flatten()  # 展开成一维数组\n",
    "print(sixDArray)  # [1 2 3 4 5 6]\n",
    "print(type(sixDArray))  # <class 'numpy.ndarray'\n",
    "\n",
    "sevenDArray = fiveDArray.reshape(2, 3)  # 调整形状\n",
    "print(sevenDArray)\n",
    "# [[1 2 3]\n",
    "#  [4 5 6]]"
   ],
   "id": "fa14f415402bc7ce",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[1 3 5 2 4 6]\n",
      "[1 2 3 4 5 6]\n",
      "<class 'numpy.ndarray'>\n",
      "[[1 3 5]\n",
      " [2 4 6]]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:02:29.671352Z",
     "start_time": "2025-01-06T16:02:29.664642Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的形状\n",
    "t = np.arange(24)  # 和列表的range函数一样，生成一个数组\n",
    "print(t)\n",
    "# [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
    "print(f\"shape{t.shape}\")  #  (24,)\n",
    "print(t.ndim)  # 1  维度\n",
    "\n",
    "# 转换成二维\n",
    "t1 = t.reshape(4, 6)\n",
    "print(t1)\n",
    "# [[ 0  1  2  3  4  5]\n",
    "#  [ 6  7  8  9 10 11]\n",
    "#  [12 13 14 15 16 17]\n",
    "#  [18 19 20 21 22 23]]\n",
    "print(f\"shape{t1.shape}\")  #  (4, 6)\n",
    "print(t1.ndim)  # 2  维度\n",
    "\n",
    "# 转换成三维\n",
    "#最前面的是零轴，轴越小，越靠外，最小的轴是最外层\n",
    "# 0轴，1轴，2轴\n",
    "t2 = t.reshape(2, 3, 4)\n",
    "print(t2)\n",
    "# [[[ 0  1  2  3]\n",
    "#   [ 4  5  6  7]\n",
    "#   [ 8  9 10 11]]\n",
    "#  [[12 13 14 15]\n",
    "#   [16 17 18 19]\n",
    "#   [20 21 22 23]]]\n",
    "print(f\"shape{t2.shape}\")  #  (2, 3, 4)\n",
    "print(t2.ndim)  # 3  维度\n",
    "\n",
    "# 转换成四维\n",
    "t3 = t.reshape(2, 2, 3, 2)\n",
    "print(t3)\n",
    "# [[[[ 0  1]\n",
    "#    [ 2  3]\n",
    "#    [ 4  5]]\n",
    "#   [[ 6  7]\n",
    "#    [ 8  9]\n",
    "#    [10 11]]]\n",
    "#  [[[12 13]\n",
    "#    [14 15]\n",
    "#    [16 17]]\n",
    "#   [[18 19]\n",
    "#    [20 21]\n",
    "#    [22 23]]]]\n",
    "print(f\"shape{t3.shape}\")  #  (2, 2, 3, 2)\n",
    "print(t3.ndim)  # 4  维度"
   ],
   "id": "356dc7ee758cc6d0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "shape(24,)\n",
      "1\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "shape(4, 6)\n",
      "2\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "shape(2, 3, 4)\n",
      "3\n",
      "[[[[ 0  1]\n",
      "   [ 2  3]\n",
      "   [ 4  5]]\n",
      "\n",
      "  [[ 6  7]\n",
      "   [ 8  9]\n",
      "   [10 11]]]\n",
      "\n",
      "\n",
      " [[[12 13]\n",
      "   [14 15]\n",
      "   [16 17]]\n",
      "\n",
      "  [[18 19]\n",
      "   [20 21]\n",
      "   [22 23]]]]\n",
      "shape(2, 2, 3, 2)\n",
      "4\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "4.5 数组转换为列表",
   "id": "863e6dbfb43b0a06"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:04:44.268958Z",
     "start_time": "2025-01-06T16:04:43.272498Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([1, 2, 3, 4, 5])\n",
    "print(a)  # [1 2 3 4 5]\n",
    "list_a = a.tolist()\n",
    "print(list_a)  # [1, 2, 3, 4, 5]\n",
    "print(type(list_a))  # <class 'list'>   "
   ],
   "id": "d6bcf4ba27a03d6f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "[1, 2, 3, 4, 5]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 5.NumPy的数据类型",
   "id": "9342c5638a841626"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:11:32.932653Z",
     "start_time": "2025-01-06T16:11:32.926823Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "\n",
    "# dtype参数指定数据类型\n",
    "f = np.array([1, 2, 3, 4, 127], dtype=np.int8)\n",
    "print(f)  # [ 1  2  3  4 127]\n",
    "print(f.itemsize)  # 1  每个元素的字节数\n",
    "print(f.dtype)  # int8\n",
    "\n",
    "# astype方法转换数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1)  # [ 1  2  3  4 127]\n",
    "print(f1.itemsize)  # 8  每个元素的字节数\n",
    "print(f1.dtype)  # int64\n"
   ],
   "id": "37390a2af5d21830",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1   2   3   4 127]\n",
      "1\n",
      "int8\n",
      "[  1   2   3   4 127]\n",
      "8\n",
      "int64\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:12:28.625021Z",
     "start_time": "2025-01-06T16:12:28.620020Z"
    }
   },
   "cell_type": "code",
   "source": "random.random()  # 随机生成0-1之间的小数",
   "id": "20c12d9b69048af9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3409698798392268"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:14:53.446794Z",
     "start_time": "2025-01-06T16:14:53.441802Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机生成100个0-1之间的小数，保留 2 位小数\n",
    "print(round(random.random(), 2))\n",
    "\n",
    "arr = np.array([round(random.random(), 2) for i in range(10)])\n",
    "print(arr)\n",
    "print(arr.dtype)  # float64\n",
    "print(arr.itemsize)  # 8  每个元素的字节数"
   ],
   "id": "c31c3842ea21ef33",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.85\n",
      "[0.36 0.42 0.68 0.28 0.96 0.42 0.22 0.75 0.94 0.83]\n",
      "float64\n",
      "8\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组和数的计算",
   "id": "34b5296dc7c2ef4e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:19:28.434731Z",
     "start_time": "2025-01-06T16:19:28.428932Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 由于numpy的广播机机制在运算过程中，加减乘除的值被广播到所有的元素上面\n",
    "# 无论多少维的ndarray都可以直接和一个常数进行运算\n",
    "t1 = np.arange(24).reshape(6, 4)\n",
    "print(t1)\n",
    "\n",
    "t2 = t1.tolist()\n",
    "print(t2)\n",
    "# print(t2+2)  #不能对列表进行直接加整数操作\n",
    "\n",
    "print(t1 + 2)  # 加2\n",
    "print(t1 - 2)  # 减2\n",
    "print(t1 * 2)  # 乘2\n",
    "print(t1 / 2)  # 除2"
   ],
   "id": "c0fa804516b97392",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "[[-2 -1  0  1]\n",
      " [ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]]\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:24:16.424411Z",
     "start_time": "2025-01-06T16:24:16.418660Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 只有形状相同的numpu数组，即shape相同，才可以进行加减乘除运算\n",
    "t1 = np.arange(24).reshape(6, 4)\n",
    "t2 = np.arange(100, 124).reshape(6, 4)\n",
    "\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1 + t2)\n",
    "print(t1 * t2)  #注意：不是线代的矩阵乘法，而是对应元素相乘\n"
   ],
   "id": "1dc5dc9517e9e215",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[100 101 102 103]\n",
      " [104 105 106 107]\n",
      " [108 109 110 111]\n",
      " [112 113 114 115]\n",
      " [116 117 118 119]\n",
      " [120 121 122 123]]\n",
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:34:15.023308Z",
     "start_time": "2025-01-06T16:34:15.017608Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 当shape不同时\n",
    "#一维数组和二维数组进行运算时，一维的元素个数和列数相等，则可以进行广播\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "t2 = np.arange(6).reshape(1, 6)\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1.shape)  # (4, 6)\n",
    "print(t2.shape)  # (1, 6)\n",
    "print(t1 + t2)  # 加法可以\n",
    "print(t1 - t2)  # 减法可以\n",
    "print((t1 - t2).shape)  # (4, 6)"
   ],
   "id": "49cc93a2432a27ac",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0 1 2 3 4 5]]\n",
      "(4, 6)\n",
      "(1, 6)\n",
      "[[ 0  2  4  6  8 10]\n",
      " [ 6  8 10 12 14 16]\n",
      " [12 14 16 18 20 22]\n",
      " [18 20 22 24 26 28]]\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n",
      "(4, 6)\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:34:31.793447Z",
     "start_time": "2025-01-06T16:34:31.787856Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4, 6)\n",
    "t2 = np.arange(4).reshape(4, 1)\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1.shape)  # (4, 6)\n",
    "print(t2.shape)  # (1, 6)\n",
    "print(t1 + t2)  # 加法可以\n",
    "print(t1 - t2)  # 减法可以\n",
    "print((t1 - t2).shape)  # (4, 6)"
   ],
   "id": "916c54b8b9d79133",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "(4, 6)\n",
      "(4, 1)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 7  8  9 10 11 12]\n",
      " [14 15 16 17 18 19]\n",
      " [21 22 23 24 25 26]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n",
      "(4, 6)\n"
     ]
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:32:08.803179Z",
     "start_time": "2025-01-06T16:32:08.789500Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 若都不是行向量或者列向量，则不能进行广播，即不能进行运算，会报错\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "t2 = np.arange(6).reshape(3, 6)\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1.shape)  # (4, 6)\n",
    "print(t2.shape)  # (1, 6)\n",
    "print(t1 + t2)  # 加法可以\n",
    "print(t1 - t2)  # 减法可以"
   ],
   "id": "8fd926157037d631",
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "cannot reshape array of size 6 into shape (3,6)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[59], line 3\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m# 若都不是行向量或者列向量，则不能进行广播，即不能进行运算，会报错\u001B[39;00m\n\u001B[0;32m      2\u001B[0m t1\u001B[38;5;241m=\u001B[39mnp\u001B[38;5;241m.\u001B[39marange(\u001B[38;5;241m24\u001B[39m)\u001B[38;5;241m.\u001B[39mreshape(\u001B[38;5;241m4\u001B[39m,\u001B[38;5;241m6\u001B[39m)\n\u001B[1;32m----> 3\u001B[0m t2\u001B[38;5;241m=\u001B[39m\u001B[43mnp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43marange\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m6\u001B[39;49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mreshape\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m6\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28mprint\u001B[39m(t1)\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28mprint\u001B[39m(t2)\n",
      "\u001B[1;31mValueError\u001B[0m: cannot reshape array of size 6 into shape (3,6)"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:37:19.713625Z",
     "start_time": "2025-01-06T16:37:19.708749Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机3维数组\n",
    "a = np.arange(24).reshape(2, 3, 4)\n",
    "b = np.arange(12).reshape(1, 3, 4)\n",
    "print(a)\n",
    "print(\"-\" * 50)\n",
    "print(b)\n",
    "print(\"-\" * 50)\n",
    "print(a + b)  # 加法可以\n",
    "print(\"-\" * 50)\n",
    "print(a - b)  # 减法可以\n",
    "print((a - b).shape)  # (2, 3, 4)"
   ],
   "id": "dadc58b57c029907",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "--------------------------------------------------\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]]\n",
      "--------------------------------------------------\n",
      "[[[ 0  2  4  6]\n",
      "  [ 8 10 12 14]\n",
      "  [16 18 20 22]]\n",
      "\n",
      " [[12 14 16 18]\n",
      "  [20 22 24 26]\n",
      "  [28 30 32 34]]]\n",
      "--------------------------------------------------\n",
      "[[[ 0  0  0  0]\n",
      "  [ 0  0  0  0]\n",
      "  [ 0  0  0  0]]\n",
      "\n",
      " [[12 12 12 12]\n",
      "  [12 12 12 12]\n",
      "  [12 12 12 12]]]\n",
      "(2, 3, 4)\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "结论：ndim要相同，可以某一个轴的size不同，但是其中一个ndarray必须对应的轴的size为1",
   "id": "216ed00329605bd5"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 练习轴",
   "id": "97f78d8a8259bb41"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:51:03.192902Z",
     "start_time": "2025-01-06T16:51:03.186057Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 按哪个轴求和，哪个轴没了\n",
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(a)\n",
    "print(\"-\" * 50)\n",
    "print(a.sum(axis=0))  # 按列求和 \n",
    "print(\"-\" * 50)\n",
    "print(a.sum(axis=1))  # 按行求和\n",
    "print(\"-\" * 50)\n",
    "print(a.sum())  # 所有元素求和\n",
    "print(\"-\" * 50)\n",
    "\n",
    "b = np.arange(24).reshape(2, 3, 4)\n",
    "print(b)\n",
    "print(\"-\" * 50)\n",
    "print(b.sum(axis=0))  # 按列求和 \n",
    "print(\"-\" * 50)\n",
    "print(b.sum(axis=1))  # 按行求和\n",
    "print(\"-\" * 50)\n",
    "print(b.sum(axis=2))  # 按深度求和\n",
    "print(\"-\" * 50)\n",
    "print(b.sum())  # 所有元素求和\n",
    "\n"
   ],
   "id": "4c65ea13b83b9dd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n",
      "[5 7 9]\n",
      "--------------------------------------------------\n",
      "[ 6 15]\n",
      "--------------------------------------------------\n",
      "21\n",
      "--------------------------------------------------\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "--------------------------------------------------\n",
      "[[12 14 16 18]\n",
      " [20 22 24 26]\n",
      " [28 30 32 34]]\n",
      "--------------------------------------------------\n",
      "[[12 15 18 21]\n",
      " [48 51 54 57]]\n",
      "--------------------------------------------------\n",
      "[[ 6 22 38]\n",
      " [54 70 86]]\n",
      "--------------------------------------------------\n",
      "276\n"
     ]
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 索引和切片",
   "id": "3a231ba0d766a241"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T16:54:57.296133Z",
     "start_time": "2025-01-06T16:54:57.291062Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(10)\n",
    "\n",
    "print(a)\n",
    "print(a[0])  # 索引第一个元素\n",
    "print(a[2:])  # 切片从第三个元素开始到最后\n",
    "print(a[::2])  # 切片从第一个元素开始，每隔2个取一个\n",
    "print(a[::-1])  # 切片从最后一个元素开始，反向取"
   ],
   "id": "3c50cd1bf27f44ef",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "0\n",
      "[2 3 4 5 6 7 8 9]\n",
      "[0 2 4 6 8]\n",
      "[9 8 7 6 5 4 3 2 1 0]\n"
     ]
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T04:15:54.687167Z",
     "start_time": "2025-01-07T04:15:54.681699Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "print(t1)\n",
    "print(\"-\" * 50)\n",
    "print(t1[1])  # 索引第1行\n",
    "print(\"-\" * 50)\n",
    "print(t1[1:])  # 连续取第1行到最后一行\n",
    "# [[ 6  7  8  9 10 11]\n",
    "#  [12 13 14 15 16 17]\n",
    "#  [18 19 20 21 22 23]]\n",
    "print(\"-\" * 50)\n",
    "print(t1[1:3])  # 连续取第1行到第2行\n",
    "# [[ 6  7  8  9 10 11]\n",
    "#  [12 13 14 15 16 17]]\n",
    "print(\"-\" * 50)\n",
    "print(t1[[0, 2, 3]])  # 读取第0行、第2行、第3行\n",
    "# [[ 0  1  2  3  4  5]\n",
    "#  [12 13 14 15 16 17]\n",
    "#  [18 19 20 21 22 23]]"
   ],
   "id": "681438b836fbb610",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[ 6  7  8  9 10 11]\n",
      "--------------------------------------------------\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T04:26:00.814883Z",
     "start_time": "2025-01-07T04:26:00.800778Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 看列表\n",
    "t2 = np.arange(24).reshape(4, 6).tolist()\n",
    "print(t2)\n",
    "print(\"-\" * 50)\n",
    "print(t2[[0, 2, 3]])  # 已经转换成一维数组，不可以进行行列索引 \n",
    "# TypeError: list indices must be integers or slices,not list"
   ],
   "id": "15794302002a5ec2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]\n",
      "--------------------------------------------------\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not tuple",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[14], line 5\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28mprint\u001B[39m(t2)\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m-\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;241m*\u001B[39m \u001B[38;5;241m50\u001B[39m)\n\u001B[1;32m----> 5\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mt2\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m2\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m]\u001B[49m) \n\u001B[0;32m      6\u001B[0m \u001B[38;5;66;03m# TypeError: list indices must be integers or slices, not list\u001B[39;00m\n",
      "\u001B[1;31mTypeError\u001B[0m: list indices must be integers or slices, not tuple"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T04:45:03.013288Z",
     "start_time": "2025-01-07T04:45:03.007670Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4, 6)\n",
    "print(t1)\n",
    "print(\"-\" * 50)\n",
    "print(t1[:, 1])  # 取第1列\n",
    "print(\"-\" * 50)\n",
    "print(t1[:, 1:])  # 取连续多列\n",
    "print(\"-\" * 50)\n",
    "print(t1[:, [0, 2, 3]])  # 取不连续的多列,取第0列、第2列、第3列\n",
    "print(\"-\" * 50)\n",
    "print(t1[2, 3])  # 取单独的一个元素\n",
    "print(\"-\" * 50)\n",
    "print(t1[2:4, 3:5])  # 取连续多行多列\n",
    "print(\"-\" * 50)\n",
    "print(t1[[0, 2, 3], [1, 3, 5]])  # 读取第0行第1列、第2行第3列、第3行第5列"
   ],
   "id": "870c1136c463fee2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[ 1  7 13 19]\n",
      "--------------------------------------------------\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "--------------------------------------------------\n",
      "15\n",
      "--------------------------------------------------\n",
      "[[15 16]\n",
      " [21 22]]\n",
      "--------------------------------------------------\n",
      "[ 1 15 23]\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 修改值",
   "id": "5bd7c75957ad1f7f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T05:10:38.558177Z",
     "start_time": "2025-01-07T05:10:38.552969Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "t = np.arange(24).reshape(4, 6)\n",
    "print(t)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# # 修改某一行的元素\n",
    "# t[1,:]=0 # 修改第一行的元素\n",
    "\n",
    "# # 修改某一列的元素\n",
    "# t[:,1]=0 # 修改第1列的元素\n",
    "\n",
    "# # 修改连续多行\n",
    "# t[1:3,:]=0 # 修改第1行到第2行的元素\n",
    "\n",
    "# # 修改连续多列\n",
    "# t[:,1:3]=0 # 修改第1列到第2列的元素\n",
    "\n",
    "# # 修改多行多列\n",
    "# t[1:3,2:5]=0 # 修改第1行到第2行第2列到第4列的元素\n",
    "\n",
    "# # 修改多个不相邻的点\n",
    "# t[[0, 1], [1, 3]] = 0\n",
    "\n",
    "# # 根据条件修改，比如将大于10的元素修改为10\n",
    "# t[t>10]=10\n",
    "\n",
    "# 使用逻辑判断\n",
    "# np.logical_and\t& # np.logical_or\t|\n",
    "# np.logical_not\t\t~\n",
    "# t[(t>2)&(t<6)]=0\t# 逻辑与，and 逻辑与操作，将满足条件的元素赋值为 0\n",
    "# t[(t<2)|(t>6)]=0\t# 逻辑或，or  逻辑或操作，将满足条件的元素赋值为 0\n",
    "t[~(t > 6)] = 0  # 逻辑非 将不满足条件的元素赋值为 0\n",
    "\n",
    "print(t)\n",
    "print(\"-\" * 50)"
   ],
   "id": "ae35ef9a8b9bcc2c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:09:35.714597Z",
     "start_time": "2025-01-07T09:09:35.560537Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "t = np.arange(24).reshape(4, 6)\n",
    "print(t < 10)"
   ],
   "id": "f7c9db3b74c2d65f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ True  True  True  True  True  True]\n",
      " [ True  True  True  True False False]\n",
      " [False False False False False False]\n",
      " [False False False False False False]]\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:10:09.012676Z",
     "start_time": "2025-01-07T09:10:09.007571Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#Python的三目运算\n",
    "a = 10\n",
    "b = 20\n",
    "c = a if a > b else b  # 等价于 c=a>b?a:b\n",
    "print(c)\n"
   ],
   "id": "21366f1c22f7284d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:15:11.312398Z",
     "start_time": "2025-01-07T09:15:11.307371Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#三目运算的numpy实现\n",
    "# np.where(condition, x, y)\n",
    "# condition：条件，True或者False组成的数组\n",
    "# x：满足条件的元素将被赋值为x\n",
    "# y：不满足条件的元素将被赋值为y\n",
    "score = np.array([[80, 90, 70, 60], [85, 95, 75, 65], [90, 95, 80, 70]])\n",
    "print(score)\n",
    "print(\"-\" * 50)\n",
    "result = np.where(score > 80, True, False)\n",
    "print(result)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 将满足条件的元素赋值为100\n",
    "score[result] = 100\n",
    "print(score)\n"
   ],
   "id": "c60a0f5d2e642337",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 90 70 60]\n",
      " [85 95 75 65]\n",
      " [90 95 80 70]]\n",
      "--------------------------------------------------\n",
      "[[False  True False False]\n",
      " [ True  True False False]\n",
      " [ True  True False False]]\n",
      "--------------------------------------------------\n",
      "[[ 80 100  70  60]\n",
      " [100 100  75  65]\n",
      " [100 100  80  70]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数据的添加、删除与去重",
   "id": "a645e1d4d33e2087"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:25:35.778030Z",
     "start_time": "2025-01-07T09:25:35.771222Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# numpy.append(arr, values, axis=None) 返回新数组\n",
    "# 函数在数组的末尾添加值。 追加操作会分配整个数组，并把原来的数组复制到新数组中。\n",
    "# 参数说明\n",
    "# arr：原数组\n",
    "# values：要向arr添加的值，需要和arr形状相同（除了要添加的轴） \n",
    "# axis：默认为 None。当axis无定义时，是横向加成，返回总是为一维数组！当axis有定义的时候，分别为0和1的时候。当axis有定义的时候，分别为0和1的时候（列数要相同）。当axis为1时，数组是加在右边（行数要相同\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "print(a)\n",
    "# [[1 2 3]\n",
    "#  [4 5 6]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(np.append(a, [7, 8, 9]))  # ndarray会被展平\n",
    "# [1 2 3 4 5 6 7 8 9]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# a数组没有改变，所以append操作返回的是新数组\n",
    "print(a)\n",
    "#[[1 2 3]\n",
    "#  [4 5 6]]\n",
    "print(\"-\" * 50)"
   ],
   "id": "6ad23786dc7277af",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "--------------------------------------------------\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:27:13.091071Z",
     "start_time": "2025-01-07T09:27:13.086500Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b = np.array([[7, 8, 9]])\n",
    "print(b.ndim)  # 2\n",
    "print(b)"
   ],
   "id": "858bc5e13ca3f512",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "[[7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:33:27.268870Z",
     "start_time": "2025-01-07T09:33:27.263172Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('沿轴 0 添加元素：')  #往哪个轴添加，那个轴size就变大\n",
    "print(a)\n",
    "# [[1 2 3]\n",
    "#  [4 5 6]]\n",
    "print(\"-\" * 50)\n",
    "# 注意：当指定轴添加时，需要添加的元素的shape要和原数组的shape保持一致\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))\n",
    "# [[1 2 3]\n",
    "#  [4 5 6]\n",
    "#  [7 8 9]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print('沿轴 1 添加元素：')\n",
    "print(a)\n",
    "# [[1 2 3]\n",
    "#  [4 5 6]]\n",
    "print(\"-\" * 50)\n",
    "# 注意：当指定轴添加时，需要添加的元素的shape要和原数组的shape保持一致\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))\n",
    "# [[1 2 3 5 5 5]\n",
    "#  [4 5 6 7 8 9]]"
   ],
   "id": "e8b4308c05a1c3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "--------------------------------------------------\n",
      "沿轴 1 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:47:31.605318Z",
     "start_time": "2025-01-07T09:47:31.598519Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# numpy.insert(arr, obj, values, axis=None) 返回新数组\n",
    "# 函数在数组的指定位置插入值。 \n",
    "# 参数说明\n",
    "# arr：原数组\n",
    "# obj：整数或者整数数组，指定要插入的位置。如果是整数，则表示在第几个位置插入，如果是整数数组，则表示在第几个位置插入多个值。\n",
    "# values：要插入的值，需要和arr形状相同（除了要插入的轴） \n",
    "# axis：默认为 None。当axis无定义时，是横向插入，返回总是为一维数组！当axis有定义的时候，分别为0和1的时候。当axis有定义的时候，分别为0和1的时候（列数要相同）。当axis为1时，数组是插入在右边（行数要相同\n",
    "\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print(a)\n",
    "# [[1 2]\n",
    "#  [3 4]\n",
    "#  [5 6]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 未传递 Axis 参数。 \n",
    "# 在插入之前输入数组会被展开，然后在第二个参数位置插入值。 \n",
    "print(np.insert(a, 1, [7, 8]))\n",
    "# [1 7 8 2 3 4 5 6]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 哪个轴插入，那个轴size就变大\n",
    "# axis=0，在第obj行插入，行数+1\n",
    "print(np.insert(a, 1, [[7, 8]], axis=0))\n",
    "# [[1 2]\n",
    "#  [7 8]\n",
    "#  [3 4]\n",
    "#  [5 6]]\n",
    "print(\"-\" * 50)\n",
    "# axis=1，在第obj列插入，列数+1\n",
    "print(np.insert(a, 1, [[7, 8, 9]], axis=1))\n",
    "# [[1 7 2]\n",
    "#  [3 8 4]\n",
    "#  [5 9 6]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 沿轴  0 广播\n",
    "print(np.insert(a, 1, 11, axis=0))\n",
    "# [[ 1  2]\n",
    "#  [11 11]\n",
    "#  [ 3  4]\n",
    "#  [ 5  6]]\n",
    "print(\"-\" * 50)\n",
    "# 沿轴  1 广播\n",
    "print(np.insert(a, 1, 11, axis=1))\n",
    "# [[ 1 11  2]\n",
    "#  [ 3 11  4]\n",
    "#  [ 5 11  6]]\n",
    "print(\"-\" * 50)\n",
    "print(np.insert(a, 1, [11, 12, 13], axis=1))\n",
    "# [[ 1 11  2]\n",
    "#  [ 3 12  4]\n",
    "#  [ 5 13  6]]\n",
    "print(\"-\" * 50)"
   ],
   "id": "2cfbabcf455e2b2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "--------------------------------------------------\n",
      "[1 7 8 2 3 4 5 6]\n",
      "--------------------------------------------------\n",
      "[[1 2]\n",
      " [7 8]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "--------------------------------------------------\n",
      "[[1 7 2]\n",
      " [3 8 4]\n",
      " [5 9 6]]\n",
      "--------------------------------------------------\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "--------------------------------------------------\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n",
      "--------------------------------------------------\n",
      "[[ 1 11  2]\n",
      " [ 3 12  4]\n",
      " [ 5 13  6]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:52:14.075907Z",
     "start_time": "2025-01-07T09:52:14.069803Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# numpy.delete(arr, obj, axis=None) 返回新数组\n",
    "# 函数删除数组的指定位置的值。 \n",
    "# 参数说明\n",
    "# arr：原数组\n",
    "# obj：整数或者整数数组，指定要删除的位置。如果是整数，则表示删除第几个位置，如果是整数数组，则表示删除第几个位置的多个值。\n",
    "# axis：默认为 None。当axis无定义时，是横向删除，返回总是为一维数组！当axis有定义的时候，分别为0和1的时候。当axis有定义的时候，分别为0和1的时候（列数要相同）。当axis为1时，数组是删除在右边（行数要相同\n",
    "\n",
    "a=np.arange(12).reshape(3,4)\n",
    "print(a)\n",
    "# [[ 0  1  2  3]\n",
    "#  [ 4  5  6  7]\n",
    "#  [ 8  9 10 11]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 未传递 Axis 参数。 在删除之前输入数组会被展开，然后在第二个参数位置删除值。 \n",
    "print(np.delete(a, 5))\n",
    "# [ 0  1  2  3  4  6  7  8  9 10 11]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 哪个轴删除，那个轴size就变小\n",
    "print(np.delete(a, 1, axis=0))\n",
    "# [[ 0  1  2  3]\n",
    "#  [ 8  9 10 11]]\n",
    "print(\"-\" * 50)\n",
    "print(np.delete(a, 1, axis=1))\n",
    "# [[ 0  2  3]\n",
    "#  [ 4  6  7]\n",
    "#  [ 8 10 11]]\n",
    "print(\"-\" * 50)"
   ],
   "id": "b1d551742b82e928",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "--------------------------------------------------\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3]\n",
      " [ 8  9 10 11]]\n",
      "--------------------------------------------------\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:15:08.258160Z",
     "start_time": "2025-01-07T10:15:08.251157Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)\n",
    "# 函数返回数组中唯一的元素。 \n",
    "# 参数说明\n",
    "# ar：输入数组。\n",
    "# return_index：默认为 False。如果为 True，则返回索引数组，其内容是输入数组中每个唯一元素的索引。\n",
    "# return_inverse：默认为 False。如果为 True，则返回逆索引数组，其内容是每个输入数组元素在输出数组中的索引。\n",
    "# return_counts：默认为 False。如果为 True，则返回计数数组，其内容是每个唯一元素在输入数组中出现的次数。\n",
    "# axis：默认为 None。如果 axis 为 None，则对整个数组进行处理；如果 axis 为整数，则对该轴进行处理。\n",
    "\n",
    "a=np.array([5, 2, 6, 2, 7, 5, 6, 9, 8, 2])\n",
    "\n",
    "print(a)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 去除重复元素，输出的是有序序列\n",
    "print(np.unique(a))\n",
    "# [2 5 6 7 8 9]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 去重数组的索引数组\n",
    "# 输出的是原数组中每个元素在去重后数组中的索引,接收时需要解包\n",
    "u,indices=np.unique(a,return_index=True)\n",
    "print(u)\n",
    "# [2 5 6 7 8 9]\n",
    "print(indices)\n",
    "# [0 1 3 4 7 8]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 原数组元素在去重后数组中的索引\n",
    "u,indices=np.unique(a,return_inverse=True)\n",
    "print(u)\n",
    "# [2 5 6 7 8 9]\n",
    "print(indices)\n",
    "# [5 2 6 2 7 5 6 9 8 2] 原数组，用来对比看\n",
    "# [0 1 2 2 3 4 4 5 6 1]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 原数组中每个元素出现的次数\n",
    "u,indices=np.unique(a,return_counts=True)\n",
    "print(u)\n",
    "# [2 5 6 7 8 9]\n",
    "print(indices)\n",
    "# [3 2 2 1 1 1]\n",
    "print(\"-\" * 50)"
   ],
   "id": "4312b58eccd51a35",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 2 6 2 7 5 6 9 8 2]\n",
      "--------------------------------------------------\n",
      "[2 5 6 7 8 9]\n",
      "--------------------------------------------------\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 4 8 7]\n",
      "--------------------------------------------------\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 0 3 1 2 5 4 0]\n",
      "--------------------------------------------------\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy的数学计算",
   "id": "6b23407eb665b11d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:29:59.048052Z",
     "start_time": "2025-01-07T10:29:59.042213Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "print(score)\n",
    "# [[80 88]\n",
    "#  [82 81]\n",
    "#  [75 81]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 1. 获取所有数据最大值\n",
    "print(np.max(score))  # 88\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 2. 获取某一个轴上的数据最大值，对应的轴就消失\n",
    "res=np.max(score,axis=0)\n",
    "print(res)\n",
    "# [82 88]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 3. 获取最小值\n",
    "print(np.min(score))\n",
    "print(\"-\" * 50) # 75\n",
    "\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "res=np.min(score,axis=1)\n",
    "print(res)\n",
    "# [80 81 75]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 5. 数据的比较\n",
    "# # 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "# maximize(a,b) 计算 a 和 b 中的每一个元素的最大值，返回一个新的数组\n",
    "res=np.maximum([-2, -1, 0, 1, 2], 0)\n",
    "print(res)\n",
    "# [0 0 0 1 2]\n",
    "print(\"-\" * 50)\n",
    "# minimize(a,b) 计算 a 和 b 中的每一个元素的最小值，返回一个新的数组\n",
    "res=np.minimum([-2, -1, 0, 1, 2], 0)\n",
    "print(res)\n",
    "# [-2 -1 0 0 0]\n",
    "print(\"-\" * 50)\n",
    "res=np.maximum([-2, -1, 4, 1, 2], [1, 2, 3, 4, 5])\n",
    "# [1 2 4 4 5]\n",
    "print(res)"
   ],
   "id": "c836c113edefcee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "--------------------------------------------------\n",
      "88\n",
      "--------------------------------------------------\n",
      "[82 88]\n",
      "--------------------------------------------------\n",
      "75\n",
      "--------------------------------------------------\n",
      "[80 81 75]\n",
      "--------------------------------------------------\n",
      "[0 0 0 1 2]\n",
      "--------------------------------------------------\n",
      "[-2 -1  0  0  0]\n",
      "--------------------------------------------------\n",
      "[1 2 4 4 5]\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:39:06.382856Z",
     "start_time": "2025-01-07T10:39:06.376856Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 6. 计算平均值\n",
    "res=np.mean(score)  # 计算所有元素的平均值\n",
    "print(res)\n",
    "res=np.mean(score,axis=0)  # 计算每一列的平均值\n",
    "print(res)\n",
    "res=np.mean(score,axis=1)  # 计算每一行的平均值\n",
    "print(res)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 7. 求前缀和\n",
    "arr=np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(arr)\n",
    "# [[1 2 3]\n",
    "#  [4 5 6]]\n",
    "print(arr.cumsum(axis=0))\n",
    "# [[1 2 3]\n",
    "#  [5 7 9]]\n",
    "print(arr.cumsum(axis=1))\n",
    "# [[ 1  3  6]\n",
    "#  [ 4  9 15]]"
   ],
   "id": "c9cd7ce9f57096e3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "81.16666666666667\n",
      "[79.         83.33333333]\n",
      "[84.  81.5 78. ]\n",
      "--------------------------------------------------\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:44:49.505487Z",
     "start_time": "2025-01-07T10:44:49.499405Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 8. argmin求最小值索引\n",
    "# argmin(a, axis=None, out=None)\n",
    "# 返回数组中最小值的索引。\n",
    "# 参数说明\n",
    "# a：输入数组。\n",
    "# axis：默认为 None。如果 axis 为 None，则对整个数组进行处理；如果 axis 为整数，则对该轴进行处理。\n",
    "# out：默认为 None。输出数组，可选\n",
    "result=np.argmin(score,axis=0)  \n",
    "res = np.min(score, axis=0)  \n",
    "print(result, res)\n",
    "\n",
    "# 9. 求每一列的标准差（这里是总体标准差）\n",
    "# 标准差是一组数据平均值分散程度的一种度量。一个较大的标准差，代表大部分数值和其平均值之间差异较大；\n",
    "# 一个较小的标准差，代表这些数据较接近平均值反应出数据的波动稳定情况，越大表示波动越大，越不稳定。\n",
    "res=np.std(score,axis=0)  # 计算所有元素的标准差\n",
    "print(res)\n",
    "\n",
    "# 10. 极 值\n",
    "result = np.ptp(score, axis=None)  #就是最大值和最小值的差\n",
    "print(result)"
   ],
   "id": "56ad14bcd8f868c3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 1] [75 81]\n",
      "[2.94392029 3.29983165]\n",
      "13\n"
     ]
    }
   ],
   "execution_count": 65
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
